package com.briup.day34;

import java.io.File;
import java.io.IOException;


import org.junit.Test;
/**
 * 文件增删改查
 * JUnit测试
 * @author 王瑞鹏
 * */

public class FileDemo3 {
	
	public static void main(String[] args) {
		
	}
	
	//创建文件
	@Test
	public void test1() {
		File file = new File("src/com/briup/day34/a.txt");
		try {
			file.createNewFile();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println(file.exists());
	}
	
	//创建目录
	@Test
	public void test2() {
		File file = new File("src/com/briup/day35");
		file.mkdir();
	}
	
	//文件修改-重命名
	@Test
	public void test3() {
		File file = new File("src/com/briup/day35/sorce.txt");
		
		File targetFile = new File("src/com/briup/day35/target.txt");
		boolean result = file.renameTo(targetFile);
		System.out.println(result);
	}
	
	//文件查看
	@Test
	public void test4() {
		File file = new File("src/com/briup/day34/hello.txt");
		
		File absFile = file.getAbsoluteFile();
		File ParentFile = absFile.getParentFile().getParentFile();
		
		System.out.println(ParentFile);
		System.out.println(absFile);
		
	}
	
	//找所有子文件  listFiles()相关方法
	@Test
	public void test5() {
		File[] roots = File.listRoots();   //根目录
		for (File root : roots) {
			System.out.println(root.getPath());
		}
		File file = new File("src/com/briup/day34");
		String[] fileNames = file.list();
		for (String string : fileNames) {
			System.out.println(string);
		}
		
	}
	
	@Test
	public void test6() {
		File dir = new File("src/com/briup/day34");
		File[] files = dir.listFiles();
		for (File file : files) {
			System.out.println(file.getPath());
			File[] childFiles = file.listFiles();
			for(File childFile :childFiles) {
				if(childFile.isDirectory()) {
				System.out.println("______________________________");
				System.out.println(childFile.getPath());
				}
			}
		}
		
	}
	
	//递归遍历1+2+3+.....+100
	@Test
	public void test7() {
		System.out.println(add(100));
		System.out.println(add2(1));
	}
	
	public int add(int i) {
		if (i == 1) {
			return 1;
		}
		return i + add(i-1);
	}
	
	public int add2(int i) {
		if (i == 100) {
			return 100;
		}
		return i + add2(i+1);
	}
	
	
	
	
	

}
